<%@ page language="java"  pageEncoding="UTF-8" %>
<%@ page import="com.shopping.modean.*" %>
<%@ page import="com.shopping.service.*" %>
<%@ page import="java.util.*" %>

<%
  request.setCharacterEncoding("UTF-8");
  String action = request.getParameter("action");
  if(action != null && action.trim().equals("register")) {
	  String username = request.getParameter("username");
	  String password = request.getParameter("password");
	  String password2 = request.getParameter("password2");
	  String phone = request.getParameter("phone");
	  String addr = request.getParameter("addr");
	  
	  //TODO 后台检查数据准确性
	  User u = new User(username,password,phone,addr);
	  UserService userService = new UserService();
	  userService.save(u);
	  
	  //重定向到个人服务页面
	  response.sendRedirect("index.jsp");
  }

%>

<!DOCTYPE html>
<html>
<head>
  <title>会员注册</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <!-- <script type="text/javascript" src="script/regcheckdata.js"></script> -->
  <script type="text/javascript">
     var req;
     function validate() {
    	var idField = document.getElementById("username");
    	var url = "validate.jsp?" + escape(idField.value);
    	if(window.XMLHttpRequest) {
    		req = new XMLHttpRequest();
    	} else if(window.ActiveXObject) {
    		req = new ActiveXObject();
    	} else {
    		alert("程序出错!");
    	}
    	req.open("GET",url,true);
    	req.onreadystatechange = callback;
    	req.send(null);
    	
     }
     
     function callback() {
    	 if(req.readyState == 4) {
    		 if(req.status == 200) {
    			 var msg = req.responseXML.getElementsByTagName("msg")[0];
    			 setMsg(msg.childNodes[0].nodeValue);
    		 }
    	 }
     }
  
     function setMsg(msg) {
    	 if(msg == "invalid") {
    	     document.getElementById("usermsg").innerHTML = "<font color='red'>已存在</font>";
    	 } else {
    		 document.getElementById("usermsg").innerHTML = "<font color='green'>通过</font>"; 
    	 }
     }
  </script>

</head>

<body>

  <form method="post" name="register" action="user_register.jsp" onSubmit="return checkdata(this)">
    <input type="hidden" name="action" value="register" />
	<table class="tableborder" align="center" cellpadding="4" cellspacing="1" width="97%">
      <tbody>
        <tr>
          <td colspan="2" class="header">注册 - 填写内容</td>
        </tr>
        <tr>
          <td class="altbg1" width="21%"><span style="color:red">*</span>用户名:</td>
          <td class="altbg2">
            <input id="username" name="username" size="25" maxlength="25" type="text" onblur="validate()"> 
            <span id="usermsg"></span>
          </td>
        </tr>
		<tr>
          <td class="altbg1"><span style="color:red">*</span>密码:</td>
          <td class="altbg2"><input name="password" size="25" type="password" value="" /></td>
        </tr>
        <tr>
          <td class="altbg1"><span style="color:red">*</span>确认密码:</td>
          <td class="altbg2"><input name="password2" size="25" type="password"></td>
        </tr>
        <tr>
          <td class="altbg1">&#30005;&#35805;:</td>
          <td class="altbg2"><input name="phone" type="text" id="phone" size="25"></td>
        </tr>
        <tr>
          <td class="altbg1" valign="top">送货地址:</td>
          <td class="altbg2"><textarea name="addr" cols="60" rows="5" id="addr"></textarea></td>
        </tr>
        <tr>
          <td class="altbg1"></td>
          <td class="altbg2"><input name="regsubmit" value="提 &nbsp; 交" type="submit"></td>
        </tr>
      </tbody>
    </table>
    
  </form>
  
</body>
</html>